package org.javaboy.order.service;

import io.seata.rm.tcc.api.BusinessActionContext;
import org.javaboy.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderService {
    @Autowired
    AccountService accountService;
    @Autowired
    OrderMapper orderMapper;

    public boolean createOrder(BusinessActionContext actionContext, Integer userId, Integer commodityId, Integer count) {
        return accountService.deduceAccount(actionContext, userId, count * 100.0);
    }

    public boolean commit(BusinessActionContext actionContext) {
        Integer userId = ((Integer) actionContext.getActionContext("userId"));
        Integer count = ((Integer) actionContext.getActionContext("count"));
        Integer commodityId = ((Integer) actionContext.getActionContext("commodityId"));
        return orderMapper.createOrder(userId, commodityId, count, 100.0 * count) == 1;
    }

    public boolean rollback(BusinessActionContext actionContext) {
        return true;
    }
}
